package com.example.developdemo.service.impl;

import com.example.developdemo.dto.Page;
import com.example.developdemo.entity.Book;
import com.example.developdemo.mapper.BookMapper;
import com.example.developdemo.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BookServiceImpl implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Override
    public void addBook(Book book) {
        bookMapper.addBook(book);
    }

    @Override
    public void deleteBookByBookId(Integer bookId) {
        bookMapper.deleteBookByBookId(bookId);

    }

    @Override
    public void batchDeleteBook(List<Integer> bookIds) {
        bookMapper.batchDeleteBok(bookIds);

    }

    @Override
    public void updateBook(Book book) {
        bookMapper.updateBook(book);

    }

    @Override
    public Page getBooksPage(Integer pageNo, Integer pageSize, String bookName, Integer categoryId, Integer status, Date start, Date end) {
        Page page =new Page();
        PageHelper.startPage(pageNo,pageSize);
        List<Book> booksByParams = bookMapper.getBooksByParams(bookName, categoryId, status, start, end);
        PageInfo<Book> bookPageInfo = new PageInfo<>(booksByParams);
        page.setPage(bookPageInfo.getList());
        page.setPageNo(bookPageInfo.getPageNum());
        page.setPageSize(bookPageInfo.getPageSize());
        page.setHasPre(bookPageInfo.isHasPreviousPage());
        page.setHasNext(bookPageInfo.isHasNextPage());
        page.setPageCount(bookPageInfo.getPages());
        page.setTotal(bookPageInfo.getTotal());
        return page;
    }
}
